﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NLog;

namespace Cef.Utility.Helper
{
    public static class DynamicConfigHelper
    {
        private static object lockObject=new object();
        private static Dictionary<string,string> config;
        private const string fileName = "dynamicConfig.json";
        private const string defaultPrinterKey = "DefaultPrinter";
        private static NLog.Logger logger = LogManager.GetCurrentClassLogger();
        private static void Reload()
        {
            lock (lockObject)
            {
                var path = Directory.GetCurrentDirectory() + $"\\{fileName}";
                FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
                StreamReader sr = new StreamReader(fs, Encoding.UTF8);
                var json = sr.ReadToEnd();
                sr.Close();
                sr.Dispose();
                fs.Close();
                fs.Dispose();
                config = JsonConvert.DeserializeObject<Dictionary<string,string>>(json);
                if (config == null)
                {
                    FileStream wfs = new FileStream(path, FileMode.OpenOrCreate,FileAccess.Write);
                    config = new Dictionary<string, string>();
                    json = JsonConvert.SerializeObject(config);
                    StreamWriter sw = new StreamWriter(wfs, Encoding.UTF8);
                    sw.Write(json);
                    sw.Close();
                    sw.Dispose();
                }
            }
        }

        private static void Save()
        {
            var path = Directory.GetCurrentDirectory() + $"\\{fileName}";
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
            var json = JsonConvert.SerializeObject(config);
            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
            sw.Write(json);
            sw.Close();
            sw.Dispose();
            fs.Close();
            fs.Dispose();
        }

        private static string GetConfig(string key)
        {
            Reload();
            if (config.ContainsKey(key))
            {
                return config[key];
            }
            return null;
        }
        private static bool SetConfig(string key,string value)
        {
            lock (lockObject)
            {
                try
                {
                    Reload();
                    if (config.ContainsKey(key))
                    {
                        config[key] = value;
                    }
                    else
                    {
                        config.Add(key, value);
                    }
                    
                    Save();
                    return true;
                }
                catch (Exception e)
                {
                    logger.Error(e);
                    return false;
                }
            }
        }

        public static string GetDefaultPrinter()
        {
            return GetConfig(defaultPrinterKey);
        }

        public static bool SetDefaultPrinter(string printerName)
        {
            return SetConfig(defaultPrinterKey, printerName);
        }
    }
}
